﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GolfLeague.Models;
using GolfLeague.EditModels;
using GolfLeague.ViewModels;

namespace GolfLeague.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class UsersController : Controller
    {
        UsersInterface usersInterface = new UsersInterface();

        //
        // GET: /Users/

        public ActionResult Index()
        {
            UsersViewModel usersViewModel = usersInterface.GetUserList();

            return View(usersViewModel);
        }


        //
        // GET: /Users/CreateUser/

        public ActionResult CreateUser()
        {
            return View();
        }

        //
        // GET: /Users/EditUser/guid

        public ActionResult EditUser(Guid id)
        {
            UserEditModel userEditModel = new UserEditModel();
            userEditModel.User = usersInterface.GetUser(id);
            List<RoleListDropdown> currentRoles = new List<RoleListDropdown>();
            foreach(aspnet_Roles role in userEditModel.User.aspnet_Roles)
                currentRoles.Add(new RoleListDropdown
                {
                    RoleID = role.RoleId,
                    RoleName = role.RoleName
                });
            userEditModel.CurrentRoles = currentRoles;

            List<RoleListDropdown> availableRoles = new List<RoleListDropdown>();
            foreach (aspnet_Roles role in usersInterface.GetRoles().Except(userEditModel.User.aspnet_Roles))
                availableRoles.Add(new RoleListDropdown
                {
                    RoleID = role.RoleId,
                    RoleName = role.RoleName
                });

            userEditModel.AvailableRoles = availableRoles;
            return View(userEditModel);
        }

        //
        // POST: /Users/EditUser/
        [HttpPost]
        public ActionResult EditUser(Guid id, UserEditModel userEditModel)
        {
            var updateUser = usersInterface.GetUser(id);
            updateUser.UserName = userEditModel.User.UserName;
            updateUser.aspnet_Roles.Clear();
            if(userEditModel.UserRoles != null)
                foreach (Guid roleID in userEditModel.UserRoles)
                    updateUser.aspnet_Roles.Add(usersInterface.GetRole(roleID));
            if (TryUpdateModel(updateUser))
            {
                usersInterface.Save();
                return RedirectToAction("Index");
            }
            else
            {
                return View(userEditModel);
            }
            
        }

        //
        // POST: /Users/DeleteUser/5
        [HttpPost]
        public ActionResult DeleteUser(Guid id)
        {
            var user = usersInterface.GetUser(id);
            usersInterface.DeleteUser(user);
            usersInterface.Save();

            return RedirectToAction("Index");
        }

    }
}
